# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

include $(NACL_SDK_ROOT)/tools/common.mk

TARGET = ruby
LIBS = ruby-static readline ncurses ppapi_simple tar nacl_io ppapi
NACL_LDFLAGS += $(NACL_CLI_MAIN_LIB)
PNACL_LDFLAGS += $(NACL_CLI_MAIN_LIB)
ifeq ($(TOOLCHAIN),glibc)
LIBS += util dl rt crypt
NACL_LDFLAGS += -Wl,-export-dynamic
else
LIBS += c glibc-compat
endif
SOURCES = main.c
INC_PATHS = $(NACLPORTS_INCLUDE)/ruby-2.0.0
ifeq ($(NACL_ARCH),x86_32)
INC_PATHS += $(NACLPORTS_INCLUDE)/ruby-2.0.0/i686-nacl
else
ifeq ($(NACL_ARCH),pnacl)
INC_PATHS += $(NACLPORTS_INCLUDE)/ruby-2.0.0/le32-nacl
else
INC_PATHS += $(NACLPORTS_INCLUDE)/ruby-2.0.0/$(NACL_ARCH)-nacl
endif
endif
INSTALL_DIR = $(NACL_PACKAGES_PUBLISH)/ruby/$(TOOLCHAIN)

ifeq ($(TOOLCHAIN),pnacl)
EXEEXT=.pexe
else
EXEEXT=.nexe
endif

# We want the nmf to contain all the .nexe we have previously
# built as well as the one currently being built, so we modify
# the list of executables that we pass to create_nmf.
ifneq ($(TOOLCHAIN),pnacl)
EXECUTABLES += $(wildcard $(OUTDIR)/$(TARGET)_x86_64$(EXEEXT))
EXECUTABLES += $(wildcard $(OUTDIR)/$(TARGET)_x86_32$(EXEEXT))
EXECUTABLES += $(wildcard $(OUTDIR)/$(TARGET)_arm$(EXEEXT))
endif

# Build rules generated by macros from common.mk:

$(foreach src,$(SOURCES),$(eval $(call COMPILE_RULE,$(src),$(CFLAGS))))

ifeq ($(CONFIG),Release)
$(eval $(call LINK_RULE,$(TARGET)_unstripped,$(SOURCES),$(LIBS),$(DEPS)))
$(eval $(call STRIP_RULE,$(TARGET),$(TARGET)_unstripped))
else
$(eval $(call LINK_RULE,$(TARGET),$(SOURCES),$(LIBS),$(DEPS)))
endif

$(eval $(call NMF_RULE,$(TARGET),))

DATA_ARCHIVE = $(INSTALL_DIR)/rbdata-$(NACL_ARCH).tar
ASSETS = bin/irb lib/ruby/2.0.0
ifeq ($(TOOLCHAIN),glibc)
EXTRA_LIBS = lib/libz.so.1 lib/libreadline.so lib/libncurses.so.5
endif

CHROMEAPPS = $(NACL_SRC)/third_party/libapps/
LIB_DOT = $(CHROMEAPPS)/libdot
HTERM = $(CHROMEAPPS)/hterm

install-hterm:
	mkdir -p $(INSTALL_DIR)
	LIBDOT_SEARCH_PATH=$(CHROMEAPPS) $(LIB_DOT)/bin/concat.sh -i $(HTERM)/concat/hterm_deps.concat -o $(INSTALL_DIR)/hterm.concat.js
	LIBDOT_SEARCH_PATH=$(CHROMEAPPS) $(LIB_DOT)/bin/concat.sh -i $(HTERM)/concat/hterm.concat -o $(INSTALL_DIR)/hterm2.concat.js
	chmod +w $(INSTALL_DIR)/hterm2.concat.js $(INSTALL_DIR)/hterm.concat.js
	cat $(INSTALL_DIR)/hterm2.concat.js >> $(INSTALL_DIR)/hterm.concat.js
	rm $(INSTALL_DIR)/hterm2.concat.js
	cp ../../build_tools/naclterm.js $(INSTALL_DIR)
	cp ../../build_tools/pipeserver.js $(INSTALL_DIR)
	cp ../../build_tools/naclprocess.js $(INSTALL_DIR)
ifeq ($(TOOLCHAIN),pnacl)
	sed -i.bak 's/x-nacl/x-pnacl/g' $(INSTALL_DIR)/naclprocess.js
endif

install: install-hterm
	../../build_tools/template_expand.py manifest.json version=$(NACLPORTS_REVISION) > $(INSTALL_DIR)/manifest.json
	tar -h -c -C ${NACL_PREFIX} -f $(DATA_ARCHIVE) ${ASSETS}
ifeq ($(TOOLCHAIN),glibc)
	tar -h -r -C ${NACL_PREFIX} -f $(DATA_ARCHIVE) ${EXTRA_LIBS}
endif
	cp $(OUTDIR)/ruby*$(EXEEXT) $(INSTALL_DIR)
	rm -f $(INSTALL_DIR)/*_unstripped*$(EXEEXT)
	cp $(OUTDIR)/ruby.nmf $(INSTALL_DIR)
	cp index.html $(INSTALL_DIR)
	cp ruby.js $(INSTALL_DIR)
	cp background.js ${INSTALL_DIR}
	cp icon_16.png ${INSTALL_DIR}
	cp icon_48.png ${INSTALL_DIR}
	cp icon_128.png ${INSTALL_DIR}
ifeq ($(TOOLCHAIN),glibc)
	cp -r $(OUTDIR)/lib* $(INSTALL_DIR)
endif
	cd $(INSTALL_DIR) && rm -f ruby.zip && zip -r ruby.zip .

.PHONY: install-hterm
